Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Using The Execute Method For most FTP commands, you must use the Internet Transfer control’s Execute method. You saw this method used for HTTP transfers earlier in the chapter. With FTP, Execute is X Table 18.2 How the Internet Transfer control handles passwords.
Username Property | Password Property | Password Sent To Server
--- | --- | ---
Null or “” | Null or “” | User’s email address
Non-null string | Null or “” | “”
Null | Non-null string | Error
Non-null string | Non-null string | Password property
considerably simpler, because it uses only two of the arguments. For FTP tasks, therefore, the syntax of Execute is:
    Inet1.Execute (url, operation)
The url argument is the URL of the FTP site, including directory, if desired. Operation is a string containing the FTP command and any needed parameters. This is how FTP works: The command and any needed parameters are always together in a single string, separated by a single space. Use the GetChunk method to obtain data returned by the FTP server. I explained how to use GetChunk earlier in the chapter; the techniques are the same for FTP as for HTTP. When working with FTP connections, keep these two points in mind: • The commands you send are carried out by the remote FTP server. Although FTP syntax is fairly well standardized, some exceptions still exist out there on the Internet. If a command does not work, the problem may be at the other end. • The commands you can use depend on your privileges on the remote server. With anonymous login, all you will be able to do in most cases is download files. Permission to upload files, create new directories, delete files, and so on must be granted by the server administrator. Table 18.3 summarizes the most commonly needed FTP commands. Table 18.3 Commonly needed FTP commands.
Command | Description
--- | ---
CD path | Changes to the remote directory specified in path.
CDUP or CD.. | Changes to parent directory.
CLOSE | Closes the current FTP connection.
DELETE file1 | Deletes the remote file specified in file1.
DIR file1 or LS file1 | Searches the directory specified in file1. Wildcards are permitted using the remote host’s syntax. If file1 is not specified, returns a full listing of the current working file. DIR returns a more detailed listing than LS.
MKDIR path | Creates a remote directory as specified in path.
PUT file1 file2 or SEND file1 file2 | Copies local file (file1) to remote file (file2).
PWD | Returns the current directory name.
QUIT | Terminates the current user.
RECV file1 file2 or GET file1 file2 | Copies remote file (file1) to local file (file2).
RENAME file1 file2 | Renames the remote file (file1) to the new name (file2).
RMDIR path | Removes the remote directory specified in path.
SIZE file1 | Returns the size of the file or directory specified in file1.
An FTP Demonstration Perhaps the best way to understand using the Internet Transfer control for FTP transfers is to see it in action. The program presented in this section, FTP_DEMO, provides fundamental FTP capabilities in a relatively simple program. You can log on to any FTP server, either as a registered user or as an anonymous guest, and view a listing of the default directory. You can change to different directories, and you can download and upload files depending on your permission level. The program is shown in Figure 18.2. When programming the Internet Transfer control, you must pay strict attention to the StillExecuting property. Because the Execute method operates asynchronously, the control can still be busy executing the most recent Execute command, even though execution has returned to your program code. If your program tries to call Execute again, an error will occur. Therefore, you must check the status of the StillExecuting property and call Execute only when this property is False. You can see how I did this by looking at the listing for this program. Figure 18.2 The FTP demonstration program. I called the program FTP_DEMO. Its objects and properties are given in Listing 18.5, and its code in Listing 18.6. I am not going to walk you through all the steps of creating this program, because you should have no problem understanding how the code operates. Be aware that FTP_DEMO is a bare-bones program; although it has basic error-handling capabilities, it has no provisions for user errors, such as trying to GET a nonexistent file. Also, there are no warnings about overwriting existing files, so use caution. Listing 18.5 Objects and properties in FTP_DEMO.FRM.
    Begin VB.Form Form1
       Caption         =   “FTP Demonstration”
       StartUpPosition =   3  ‘Windows Default
       Begin VB.CommandButton Command2
          Caption         =   “Change Dir”
          Index           =   2
       End
       Begin VB.CommandButton Command2
          Caption         =   “Get”
          Index           =   1
       End
       Begin VB.CommandButton Command2
          Caption         =   “Put”
          Index           =   0
       End
       Begin VB.TextBox txtRemoteDir
       End
       Begin InetCtlsObjects.Inet Inet1
       End
       Begin VB.TextBox txtRemoteFileName
       End
       Begin VB.TextBox txtLocalFileName
       End
       Begin VB.CommandButton Command1
          Caption         =   “Quit”
          Index           =   3
       End
       Begin VB.CommandButton Command1
          Caption         =   “LogOff”
          Index           =   2
       End
       Begin VB.CommandButton Command1
          Caption         =   “Refresh”
          Index           =   1
       End
       Begin VB.CommandButton Command1
          Caption         =   “LogOn”
          Index           =   0
       End
       Begin VB.TextBox txtDirectory
          Locked          =   -1  ‘True
          MultiLine       =   -1  ‘True
          ScrollBars      =   3  ‘Both
       End
       Begin VB.TextBox txtPassword
       End
       Begin VB.TextBox txtUserName
       End
       Begin VB.TextBox txtURL
       End
       Begin VB.Label Label6
          Caption         =   “Remote directory”
       End
       Begin VB.Label Label5
          Caption         =   “Remote File Name”
       End
       Begin VB.Label Label4
          Caption         =   “Local File Name”
       End
       Begin VB.Label lblStatus
          ForeColor       =   &H000000FF&
       End
       Begin VB.Label Label3
          Caption         =   “Enter password, or your
                               e-mail address for anonymous logon”
       End
       Begin VB.Label Label2
          Caption         =   “Enter user name, or blank”
       End
       Begin VB.Label Label1
          Caption         =   “Enter FTP server URL”
       End
    End
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.